/*!
 * \file frlist_remove_from_idx.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __my_list_node_free(Frlist_node *node)
{
    frfree(node->data);
}


static void __case1(void)
{
    Frlist_header *list = NULL;

    frlist_remove_from_idx(list, 0, __my_list_node_free);

    CU_ASSERT_PTR_NULL(list);

}

static void __case2(void)
{
    Frlist_header *list = NULL;
    list = frlist_create();

    frlist_remove_from_idx(list, 0, __my_list_node_free);

    CU_ASSERT_PTR_NULL(list->head);
    CU_ASSERT_PTR_NULL(list->tail);
    CU_ASSERT_EQUAL(list->size,0);

    frlist_destroy(list,NULL);

}

static void __case3(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","1");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","2");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","3");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","4");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","5");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_remove_from_idx(list, -1, __my_list_node_free);

    CU_ASSERT_PTR_NULL(list->head);
    CU_ASSERT_PTR_NULL(list->tail);
    CU_ASSERT_EQUAL(list->size,0);

    frlist_destroy(list,__my_list_node_free);

}

static void __case4(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","1");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","2");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","3");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","4");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","5");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_remove_from_idx(list, 0, __my_list_node_free);

    CU_ASSERT_PTR_NULL(list->head);
    CU_ASSERT_PTR_NULL(list->tail);
    CU_ASSERT_EQUAL(list->size,0);

    frlist_destroy(list,__my_list_node_free);

}

static void __case5(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","1");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","2");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","3");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","4");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","5");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_remove_from_idx(list, 3, __my_list_node_free);

    list_node = frlist_node_idx(list, 0);
    CU_ASSERT_STRING_EQUAL(list_node->data,"1");

    list_node = frlist_node_idx(list, 1);
    CU_ASSERT_STRING_EQUAL(list_node->data,"2");

    list_node = frlist_node_idx(list, 2);
    CU_ASSERT_STRING_EQUAL(list_node->data,"3");

    CU_ASSERT_EQUAL(list->size,3);

    frlist_destroy(list,__my_list_node_free);

}


static void __case6(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","1");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","2");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","3");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","4");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","5");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_remove_from_idx(list, 5, __my_list_node_free);

    list_node = frlist_node_idx(list, 0);
    CU_ASSERT_STRING_EQUAL(list_node->data,"1");

    list_node = frlist_node_idx(list, 1);
    CU_ASSERT_STRING_EQUAL(list_node->data,"2");

    list_node = frlist_node_idx(list, 2);
    CU_ASSERT_STRING_EQUAL(list_node->data,"3");

    list_node = frlist_node_idx(list, 3);
    CU_ASSERT_STRING_EQUAL(list_node->data,"4");

    list_node = frlist_node_idx(list, 4);
    CU_ASSERT_STRING_EQUAL(list_node->data,"5");

    CU_ASSERT_EQUAL(list->size,5);

    frlist_destroy(list,__my_list_node_free);

}

int add_suite_frlist_remove_from_idx(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frlist frlist_remove_from_idx()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);

    return FR_SUCC;
}
